//
//  TabTable.m
//  weibo3
//
//  Created by Tian Wu on 11-4-12.
//  Copyright 2011年 nfsysu. All rights reserved.
//

#import "TabTable.h"

extern RootViewController * rootView;

@implementation TabTable

@synthesize isUserViewMode;

- (id)init
{
    self = [super init];
    if (self) {
        
        self.isUserViewMode = NO;
        
        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self 
               selector:@selector(nextPage:) 
                   name:@"toNextPage"
                 object:nil];
        [nc addObserver:self 
               selector:@selector(prePage:) 
                   name:@"toPrePage"
                 object:nil];
        [nc addObserver:self 
               selector:@selector(didRefreshList) 
                   name:@"windowBecomeKey"
                 object:nil];
    }
    
    return self;
}

- (NSInteger) currentPage{
    
    return [page intValue];
    
}
- (NSInteger) currentFunction{
    
    return [self selectedRow];
    
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView{
    [table setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
    [table setBackgroundColor:[NSColor clearColor]];
    
    return 5;    
}


- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
    
    if (aTableColumn == [[aTableView tableColumns] objectAtIndex:1]) {
        return [NSImage imageNamed:@"SelectionNipple.png"];
    }
    
    if (rowIndex == 0) {

        return [NSImage imageNamed:@"SideTweets.png"];
    }
    else if (rowIndex == 1){
        return [NSImage imageNamed:@"SideReplies.png"];
        
    }
    else if (rowIndex == 2){
        return [NSImage imageNamed:@"SideProfile.png"];
    }
    else if (rowIndex == 3){
        return [NSImage imageNamed:@"SideStar.png"];
    }
    else {
        return [NSImage imageNamed:@"SideSearch.png"];
    }
}

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification{
    
    if([[NSUserDefaults standardUserDefaults] boolForKey:@"logined"]){
        [rootView refreshStatusWithFunction:[table selectedRow] Page:1];
        [_rootViewController scrollToTop:self];
        
    }
    //[page setTitleWithMnemonic:[NSString stringWithFormat:@"%d",1]];
    
}



- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
    if (aTableColumn == [[aTableView tableColumns] objectAtIndex:1]){
        if (rowIndex != [aTableView selectedRow]) {
            [aCell setImage:nil];
        }
    }
    else{
        NSString *imgname = [NSString stringWithString:@""];
        if (rowIndex == 0) {
             imgname = [NSString stringWithString:@"SideTweets"];
        }
        else if (rowIndex == 1){
            imgname = [NSString stringWithString:@"SideReplies"];
        }
        else if (rowIndex == 2){
            imgname = [NSString stringWithString:@"SideProfile"];
        }
        else if (rowIndex == 3){
            imgname = [NSString stringWithString:@"SideStar"];
            
        }
        else{
            imgname = [NSString stringWithString:@"SideSearch"];
        }
        
        if (rowIndex == [aTableView selectedRow]) {
            imgname = [imgname stringByAppendingString:@"hi"];        
        }
        imgname = [imgname stringByAppendingString:@".png"];
        
        if (self.isUserViewMode == YES && [aTableView selectedRow] == 2 && rowIndex == [aTableView selectedRow]) {
            NSImage *image= [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:[(User *)[[[_rootViewController publicData] objectAtIndex:0] user] profile_image_url]]];
            [aCell setImage:image];
            [image release];
        }
        else{
            [aCell setImage:[NSImage imageNamed:imgname]];
        }
        
    }

}


- (IBAction) refreshTimeLine:(id)sender{
    [rootView refreshStatusWithFunction:[table selectedRow] Page:1];
    [page setTitleWithMnemonic:[NSString stringWithFormat:@"%d",1]];
    [[_rootViewController listView] scrollToRow:0];
    //[[NSSound soundNamed:@"getTimeline.aif"] play];
}

- (IBAction) nextPage: (id) sender{
    if([table selectedRow] == 4){
        return;
    }
    else{
        [_rootViewController refreshStatusWithFunction:[table selectedRow] Page:[page intValue]+1];
        [page setTitleWithMnemonic:[NSString stringWithFormat:@"%d",[page intValue]+1]];
        [_rootViewController scrollToTop:self];
    }
    
    
}

- (IBAction) prePage: (id) sender{
    if([table selectedRow] == 4){
        return;
    }
    if ([page intValue] == 1) {
        return;
    }
    else{
        [_rootViewController refreshStatusWithFunction:[table selectedRow] Page:[page intValue]-1];
        [page setTitleWithMnemonic:[NSString stringWithFormat:@"%d",[page intValue]-1]];
        [_rootViewController scrollToTop:self];
    }
}




- (void)dealloc
{
    [super dealloc];
}

@end
